﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Veteran_cms.Models;
namespace Veteran_cms.Controllers
{
    public class ManageSupportController : BaseController
    {
        Veterandb ctx = new Veterandb();
        //
        // GET: /ManageAbout/

        public ActionResult Support()
        {
            var os = (from aboutus in ctx.supports where aboutus.support_id != 0 select aboutus).SingleOrDefault();
            if (os != null)
                return View(os);
            else
            {
                os = new support();
                return View(os);
            }
        }

        [HttpPost]
        public ActionResult UpdateToOnlineSupport([Bind(Exclude = "support_id")]support os, int id)
        {
            try
            {
                //if (!IsAuthenticateAdmin())
                //    return RedirectToAction("LogOn", "DashBoard");
                EnCrypto cript = new EnCrypto();
                var currentOS = (from aboutus in ctx.supports where aboutus.support_id == id select aboutus).SingleOrDefault();
                if (currentOS != null)
                {
                   
                    UpdateModel(currentOS);
                    currentOS.mail_from_pwd = cript.Encrypt(currentOS.mail_from_pwd);
                    ctx.SaveChanges();
                    ViewData["Msg"] = "Cập nhật thành công!";
                    return View("Support",currentOS);
                }
                else
                {

                    os.mail_from_pwd = cript.Encrypt(os.mail_from_pwd);
                    ctx.AddTosupports(os);
                    ctx.SaveChanges();
                    ViewData["Msg"] = "Tạo mới thành công!";
                    return View("Support",os);
                }
            }
            catch
            {
                return View();
            }
        }
    }
}
